package com.furniture.ui.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.furniture.R;
import com.furniture.sql.Good;
import com.furniture.sql.manage.GoodManage;
import com.furniture.ui.activity.MainActivity;
import com.furniture.ui.utils.ListToJson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Bob on 2016/3/27.
 * 商品适配器
 */
public class GoodAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final String ACTION_LIKE_BUTTON_CLICKED = "action_like_button_button";
    public static final String ACTION_LIKE_IMAGE_CLICKED = "action_like_image_button";
    public static final String LOGIN_USER_NAME_TYPE = "login_user_name_type";

    private Context context;
    private List<Good> goodList = new ArrayList<Good>();

    private boolean showLoadingView = false;

    public SharedPreferences sp;

    private OnGoodItemClickListener onGoodItemClickListener;
    private GoodManage goodManage;

    public void setOnGoodItemClickListener(OnGoodItemClickListener onGoodItemClickListener) {
        this.onGoodItemClickListener = onGoodItemClickListener;
    }

    public GoodAdapter(Context context, List<Good> list) {
        this.context = context;
        this.goodList = list;
        this.goodManage = GoodManage.newInstance(context);
    }
    public GoodAdapter(Context context) {
        this.context = context;
        this.goodManage = GoodManage.newInstance(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //显示动态列表
        View view = LayoutInflater.from(context).inflate(R.layout.item_good, parent, false);
        sp = context.getSharedPreferences("mySp", Context.MODE_PRIVATE);
        CellGoodViewHolder cellGoodViewHolder = new CellGoodViewHolder(view,sp);
        setupClickableViews(view, cellGoodViewHolder);
        return cellGoodViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((CellGoodViewHolder) holder).bindView(goodList.get(position));
    }

    @Override
    public int getItemCount() {
        return goodList.size();
    }


    public List updateItems(List<Good> list) {
        goodList.clear();
        goodList.addAll(list);
        notifyDataSetChanged();
        return list;
    }

    public void showLoadingView() {
        showLoadingView = true;
        notifyItemChanged(0);
    }

    public static class CellGoodViewHolder extends RecyclerView.ViewHolder {
        //展示的图片
        @Bind(R.id.ivGoodCenter)
        ImageView ivGoodCenter;
        //图片信息
        @Bind(R.id.ivGoodBottom)
        TextView ivGoodBottom;
        //评论按钮
        @Bind(R.id.btnComments)
        ImageButton btnComments;
        //点赞按钮
        @Bind(R.id.btnLike)
        ImageButton btnLike;
        //更多按钮
        @Bind(R.id.btnMore)
        ImageButton btnMore;
        //？
        @Bind(R.id.vBgLike)
        View vBgLike;
        //点击图片后动画展现心形
        @Bind(R.id.ivLike)
        ImageView ivLike;
        //TextSwitcher组件（用于改变喜欢的人数）
        @Bind(R.id.tsLikesCounter)
        TextSwitcher tsLikesCounter;
        //用户头像
        @Bind(R.id.ivUserProfile)
        ImageView ivUserProfile;
        //图文内容整个框的SquaredFrameLayout
        @Bind(R.id.vImageRoot)
        FrameLayout vImageRoot;
        @Bind(R.id.ivUserName)
        TextView ivUserName;

        Good goodItem;

        SharedPreferences sp;

        public CellGoodViewHolder(View view, SharedPreferences sp) {
            super(view);
            this.sp = sp;
            ButterKnife.bind(this, view);
        }

        //循环输出首页展示列表
        public void bindView(Good goodItem) {
            this.goodItem = goodItem;

            SharedPreferences.Editor e = sp.edit();
            e.putString(LOGIN_USER_NAME_TYPE,goodItem.getNickName());
            e.commit();
            Log.i("NICKNAME", goodItem.getNickName());

            int adapterPosition = getAdapterPosition();
            ImageLoader.getInstance().displayImage(goodItem.getGoodPic(), ivGoodCenter);
            ImageLoader.getInstance().displayImage(goodItem.getIcon(), ivUserProfile);
            ivUserName.setText(goodItem.getNickName());
//            ImageLoader.getInstance().displayImage("http://imgsrc.baidu.com/forum/pic/item/e1fe9925bc315c6013d72aa28db1cb1349547738.jpg", ivGoodBottom);
            ivGoodBottom.setText(goodItem.getGoodMsg());
            btnLike.setImageResource(goodItem.getIsLike() ? R.drawable.ic_heart_red : R.drawable.ic_heart_outline_grey);
            tsLikesCounter.setCurrentText(vImageRoot.getResources().getQuantityString(
                    R.plurals.likes_count, Integer.valueOf(goodItem.getLikeNum() + ""), Integer.valueOf(goodItem.getLikeNum() + "")
            ));
        }



        public Good getGoodItem() {
            return goodItem;
        }
    }

    private void setupClickableViews(final View view, final CellGoodViewHolder cellGoodViewHolder) {
        //评论按钮点击监听器
        cellGoodViewHolder.btnComments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                onGoodItemClickListener.onCommentsClick(view, cellGoodViewHolder.getAdapterPosition());
            }
        });
        //更多按钮点击监听器
        cellGoodViewHolder.btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onGoodItemClickListener.onMoreClick(v, cellGoodViewHolder.getAdapterPosition());
            }
        });
        //展示的图片点击监听器
        cellGoodViewHolder.ivGoodCenter.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int adapterPosition = cellGoodViewHolder.getAdapterPosition();
                Good item = goodList.get(adapterPosition);
                if (item.getIsLike()==true){
                    item.setIsLike(false);
                    ((MainActivity) context).showDisLikedSnackBar();
                } else {
                    item.setIsLike(true);
                    ((MainActivity) context).showLikedSnackBar();
                }
                goodManage.addItem(item);
                ListToJson.goodToList(item);
                notifyItemChanged(adapterPosition, ACTION_LIKE_IMAGE_CLICKED);
            }
        });
        //喜欢按钮点击监听器
        cellGoodViewHolder.btnLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = cellGoodViewHolder.getAdapterPosition();
                Good item = goodList.get(adapterPosition);
                if (item.getIsLike()==true){
                    item.setIsLike(false);
                    ((MainActivity) context).showDisLikedSnackBar();
                } else {
                    item.setIsLike(true);
                    ((MainActivity) context).showLikedSnackBar();
                }
                notifyItemChanged(adapterPosition, ACTION_LIKE_BUTTON_CLICKED);
                goodManage.addItem(item);
            }
        });
        //用户头像点击监听器
        cellGoodViewHolder.ivUserProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onGoodItemClickListener.onProfileClick(view);
            }
        });
    }

    public interface OnGoodItemClickListener {
        void onCommentsClick(View v, int position);

        void onMoreClick(View v, int position);

        void onProfileClick(View v);
    }
}
